package com.example.train_operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.train_operate.entity.Cabinet;
import com.example.train_operate.entity.Examine;
import com.example.train_operate.mapper.CentralizedMaintenanceGuidelinesMapper;
import com.example.train_operate.service.CentralizedMaintenanceGuidelinesService;
import com.example.train_operate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.example.train_operate.util.FileUrlConstants.FILE_URL;

@Service
public class CentralizedMaintenanceGuidelinesServiceImpl extends ServiceImpl<CentralizedMaintenanceGuidelinesMapper, Examine> implements CentralizedMaintenanceGuidelinesService {
    @Autowired
    private CentralizedMaintenanceGuidelinesMapper centralizedMaintenanceGuidelinesMapper;

    @Override
    public Result addCentralizedMaintenanceGuidelines(Examine centralizedMaintenanceGuidelines) {
        try {
            QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("step", centralizedMaintenanceGuidelines.getStep());
            if (centralizedMaintenanceGuidelinesMapper.findByStep(centralizedMaintenanceGuidelines.getStep()) != null) {
                return Result.fail().message("已有此步骤，请勿重复添加");
            }
            String photoUrl = centralizedMaintenanceGuidelines.getPhotoUrl();
            if(photoUrl != null && !photoUrl.isEmpty()){
                String substring = photoUrl.substring(FILE_URL.length());
                centralizedMaintenanceGuidelines.setPhotoUrl(substring);
            }
            centralizedMaintenanceGuidelinesMapper.addByStep(centralizedMaintenanceGuidelines);
            return Result.ok().message("添加成功");
        }catch (Exception e)
        {
            return Result.fail().message(e.getMessage());
        }
    }

    @Override
    public Result updateCentralizedMaintenanceGuidelines(Examine centralizedMaintenanceGuidelines) {
        if(centralizedMaintenanceGuidelinesMapper.findByStep(centralizedMaintenanceGuidelines.getStep())==null){
            Result.ok(centralizedMaintenanceGuidelinesMapper.addByStep(centralizedMaintenanceGuidelines)).message("修改成功");
        }
        return Result.ok(centralizedMaintenanceGuidelinesMapper.updateByStep(centralizedMaintenanceGuidelines)).message("修改成功");
    }

    @Override
    public Result deleteCentralizedMaintenanceGuidelines(Integer step) {
        try {
            QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("step", step);
            Examine examine = centralizedMaintenanceGuidelinesMapper.findByStep(step);
            if (examine == null) {
                return Result.fail().message("没有此步骤");
            }
            centralizedMaintenanceGuidelinesMapper.deleteByStep(step);
            return Result.ok().message("删除成功");
        }catch (Exception e)
        {
            return Result.fail().message("出问题了");
        }
    }

    @Override
    public Result getCentralizedMaintenanceGuidelinesByStep(Integer step) {
        try {
            QueryWrapper<Cabinet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("step", step);
            Examine examine = centralizedMaintenanceGuidelinesMapper.findByStep(step);
            if (examine == null) {
                return Result.fail().message("没有此步骤");
            }
            return Result.ok(centralizedMaintenanceGuidelinesMapper.findByStep(step));
        }catch (Exception e)
        {
            return Result.fail().message("出问题了");
        }
    }

    @Override
    public Result listCentralizedMaintenanceGuidelines() {
        return Result.ok(centralizedMaintenanceGuidelinesMapper.findAll());
    }
}
